from urllib import request, parse
import requests
from lxml import etree
import ssl

# 取消 https 代理验证
ssl._create_default_https_context = ssl._create_unverified_context

douban_url = "https://movie.douban.com/j/search_subjects"


def douban(self, type, page_limit, page_start, tag, sort):
    # 报头
    user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36'
    headers = {'User-Agent': user_agent}
    try:
        params = {
            "type": type,
            "page_limit": page_limit,
            "page_start": page_start,
            "tag": parse.quote(tag),
            "sort": sort,
        }

        proxies = {
            'http': 'https://180.160.60.39:9999',
            'https': 'https://221.6.201.18:9999'
        }

        # response = requests.get(douban_url,params=params,headers=headers)
        response = requests.get(douban_url + "?type=" + type + "&tag=" + parse.quote(
            tag) + "&sort=" + sort + "&page_limit=" + page_limit + "&page_start=" + page_start, headers=headers)
        return response.json()
    except Exception:
        print(type, Exception)
        data = {"code": -1, "message": "error", "data": None}
        return data
